package com.s_recycler;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.s7.recycler.helper.LoadingHelper;
import com.s7.recycler.listener.OnLoadDataListener;
import com.s7.recycler.manager.StaggeredManager;
import com.s_recycler.adapter.LoadAdapter;
import com.s_recycler.bean.LoadBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class LoadActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView hint;
    private Button btn1, btn2, btn3;
    private SwipeRefreshLayout srl;
    private RecyclerView recycler;
    private List<LoadBean> datas;
    private LoadingHelper mHelper;

    private String[] images = {"http://b-ssl.duitang.com/uploads/item/201701/13/20170113092725_AucYf.jpeg",
            "http://b-ssl.duitang.com/uploads/item/201507/04/20150704111600_XrZ2z.jpeg",
            "http://b-ssl.duitang.com/uploads/item/201604/16/20160416203342_PUsta.thumb.700_0.jpeg",
            "http://b-ssl.duitang.com/uploads/item/201507/27/20150727234159_MKaYe.jpeg",
            "http://b-ssl.duitang.com/uploads/item/201606/10/20160610121636_i2EWn.jpeg",
            "http://b-ssl.duitang.com/uploads/item/201507/13/20150713231701_UZatw.thumb.700_0.jpeg"
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_load);

        srl = findViewById(R.id.load_srl);
        recycler = findViewById(R.id.load_recycler);
        hint = findViewById(R.id.load_hint);
        btn1 = findViewById(R.id.load_btn1);
        btn2 = findViewById(R.id.load_btn2);
        btn3 = findViewById(R.id.load_btn3);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);

        datas = new ArrayList<>();
        mHelper = new LoadingHelper.Builder<LoadBean>()
                .setPageSize(10)
                .setSwipeRefresh(srl)
                .setRecycler(recycler, new StaggeredManager(2, StaggeredManager.VERTICAL))
                .setAadditional(true)
                .setAdapter(new LoadAdapter(), true)
                .setLoadData(new OnLoadDataListener() {
                    @Override
                    public void onLoadData(int page) {
                        getData(page);
                    }
                }).build();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.load_btn1: // 线性布局
                hint.setText("当前布局--线性布局");
                mHelper.setLayoutManager(new LinearLayoutManager(this));
                break;
            case R.id.load_btn2: // Grid布局
                hint.setText("当前布局--Grid布局");
                mHelper.setLayoutManager(new GridLayoutManager(this, 2));
                break;
            case R.id.load_btn3: // 瀑布流布局
                hint.setText("当前布局--瀑布流");
                mHelper.setLayoutManager(new StaggeredManager(2, StaggeredManager.VERTICAL));
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mHelper.loadFirstPage();
    }

    private int s = 0;

    public void getData(int page) {
        if (page == 1) {
            s = 0;
        }
        int e = page * 10;
        if (page == 6) {
            e = e - 3;
        }
        datas.clear();
        for (int i = s; i < e; i++) {
            String url = images[new Random().nextInt(6)];
            datas.add(new LoadBean("你好" + i, url));
        }
        s = e;
        CountDown(3);
    }

    public void CountDown(int s) {
        new CountDownTimer(s * 1000, 1000) {
            @Override
            public void onTick(long l) {
            }

            @Override
            public void onFinish() {
                mHelper.notifyAdapter(datas);
            }
        }.start();
    }

}
